package com.hellowx.manager.controller;

import com.hellowx.manager.pojo.GoodsCategory;
import com.hellowx.manager.service.GoodsCategoryService;
import com.hellowx.manager.vo.GoodsCategoryVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * 商品分类 controller
 * @author MagicMushroom
 * @date 2021/5/22
 */
@RestController
@RequestMapping("/goods/category")
public class GoodsCategoryController {

    @Resource
    private GoodsCategoryService goodsCategoryService;

    /**
     * 获取顶级分类
     * @return 顶级分类列表
     */
    @RequestMapping("/topCategory")
    public List<GoodsCategory> selectTopLevel(){
        return goodsCategoryService.selectTopCategory();
    }

    /**
     * 获取二级分类
     * @param parentId 父分类id
     * @return 二级分类列表
     */
    @RequestMapping("/secondaryCategory")
    public List<GoodsCategory> selectSecondaryCategory(Short parentId){
        return goodsCategoryService.selectSecondaryCategory(parentId);
    }

    /**
     *  添加分类
     * @param goodsCategory 商品分类对象
     * @return map，是否成功
     */
    @RequestMapping("/addCategory")
    public Map<String, Object> addCategory(GoodsCategory goodsCategory){

        int i =goodsCategoryService.insertGoodsCategory(goodsCategory);
        Map<String, Object> map=new HashMap<>();
        if (i>0){
            map.put("message","添加成功");
        }else {
            map.put("message","添加失败");
        }
        return map;
    }

    /**
     * 查询所有商品分类
     * @return 所有商品分类列表
     */
    @RequestMapping("/categoryList")
    public List<GoodsCategoryVO> selectGoodsCategoryList(){
        return goodsCategoryService.selectCategoryList();
    }


    /**
     *  删除商品分类
     * @param id 商品分类id
     * @return 是否成功
     */
    @RequestMapping("/deleteCategory")
    public String deleteCategory(Short id){
        int i = goodsCategoryService.deleteCategory(id);
        if (i>0){
            return "删除成功!";
        }else {
            return "删除失败！！";
        }
    }


    @RequestMapping("/allCategory")
    public List<GoodsCategory> allCategory(){
        return goodsCategoryService.selectAll();
    }
}
